package com.linkwedo.saas.users;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.linkwedo.saas.domain.member.Team;
import com.linkwedo.saas.domain.member.User;

public class TeamDao extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
	.getLogger(TeamDao.class);
	// property constants


	public void save(Team transientInstance) {
		log.debug("saving Team instance");
		Session curSession = getSession();
		try {
			Transaction tr = curSession.beginTransaction();
			Set users = transientInstance.getUsers();
			Iterator itr =users.iterator();
			while(itr.hasNext()){
				User curUser = (User)itr.next();
//				User tmp = new User();
//				tmp.setId(curUser.getId());
				curSession.saveOrUpdate(curUser);
			}
			curSession.save(transientInstance);
			tr.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public void delete(Team persistentInstance) {
		log.debug("deleting com.huayanginfo.saas.domain.Team instance");
		Session curSession = getSession();
		try {
			Transaction tr = curSession.beginTransaction();
			curSession.delete(persistentInstance);
			log.debug("delete successful");
			tr.commit();
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public Team findById(java.lang.Integer id) {
		log.debug("getting Team instance with id: " + id);
		Session curSession = getSession();
		try {
			Team instance = (Team) curSession.get(
					"com.huayanginfo.saas.domain.Team", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public List findByExample(Team instance) {
		log.debug("finding Team instance by example");
		Session curSession = getSession();
		try {
			List results = curSession.createCriteria("com.huayanginfo.saas.domain.member.Team").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Team instance with property: " + propertyName
				+ ", value: " + value);
		Session curSession = getSession();
		try {
			String queryString = "from Team as model where model."
					+ propertyName + "= ?";
			Query queryObject = curSession.createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	

	public List findAll() {
		log.debug("finding all Team instances");
		Session curSession = getSession();
		try {
			String queryString = "from Team";
			Query queryObject = curSession.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public Team merge(Team detachedInstance) {
		log.debug("merging Team instance");
		Session curSession = getSession();
		try {
			Transaction tx = curSession.beginTransaction();
			Team result = (Team) curSession.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public void attachDirty(Team instance) {
		log.debug("attaching dirty Team instance");
		Session curSession = getSession();
		try {
			Transaction tx = curSession.beginTransaction();
			curSession.saveOrUpdate(instance);
			tx.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public void attachClean(Team instance) {
		log.debug("attaching clean Team instance");
		Session curSession = getSession();
		try {
			Transaction tx = curSession.beginTransaction();
			curSession.lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}
	
}
